/*!
 * \file ems_text_message.c
 *
 * \brief 主要对应TibjmsTextMessage类
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.
 *
 * \version 1.0
 * \author songbb
 * \date 2016/10/24
 *
 */

#include "conntrack.h"
#include "frct_comm.h"

#include "ems_core.h"
#include "ems_comm.h"
#include "ems_text_message.h"
#include "ems_xstream.h"
#include "ems_ystream.h"


int ems_text_readBody(Conntrack *p_st_conn
                    , L7_ems_packet *p_st_ems_packet
                    , int i_compressed
                    , Frreport_body_data *p_st_body
                    , Frhash_header *p_st_hash_report)
{
    EMS_PRINT_INFO("ems_text_readBody...\n");
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ems_private *p_st_session_private = (L7session_ems_private *) frct_get_session_private(p_st_session);
    L7session_ems *p_st_sess = p_st_session_private->p_st_sess;
    char *p_text = NULL;
    int i_text_len;
    int i_body_size;
    char *p_inflated = NULL;
    int i_inflated_len = 0;

    if (p_st_sess->model_type == EMS_MODEL_TYPE_UNKNOWN)
        goto error;

    if (ems_readInt(p_st_ems_packet, &i_body_size) != FR_SUCC)
        goto error;

    if (i_compressed == FR_NO)
    {
        if (ems_readString(p_st_sess, p_st_ems_packet, FR_NO, &p_text, &i_text_len) != FR_SUCC)
            goto error;
    }
    else
    {
        if (ems_readCompressed(p_st_ems_packet, &p_inflated, &i_inflated_len) != FR_SUCC)
            goto error;
        EMS_PRINT_INFO("unCompressed ok\n");
        if (p_inflated != NULL && i_inflated_len != 0)
        {
            int i_offset = 0;
            if (ems_y_readString(p_st_sess, p_inflated, i_inflated_len, &i_offset, FR_NO, &p_text, &i_text_len) != FR_SUCC)
                goto error;
        }
    }

    EMS_PRINT_FATAL("_text_:[%s]\n", p_text);

    if (p_st_body)
    {
        if (frreport_body_l7_add_str(p_st_body, "tibcoems.method", "text") != FR_SUCC)
            goto error;

        if (p_st_sess->model_type == EMS_MODEL_TYPE_PRODUCER)
        {
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_REQ".tibcoems.text", p_text == NULL ? "" : p_text) != FR_SUCC)
                goto error;

            // 上报数据
            if (ems_report_msg_data_req(p_st_conn, p_st_ems_packet, p_st_body, p_st_hash_report) != FR_SUCC)
                goto error;

            frreport_body_l7list_free(&(p_st_session_private->p_report_comm));
            frreport_body_l7list_free(&(p_st_session_private->p_report_req));
            p_st_session_private->p_report_comm = FRREPORT_BODY_L7LIST_CLONE_COMM(p_st_body);
            p_st_session_private->p_report_req = FRREPORT_BODY_L7LIST_CLONE_REQ(p_st_body);
            p_st_session_private->st_req_end_timestamp.tv_sec = p_st_ems_packet->st_timestamp_last.tv_sec;
            p_st_session_private->st_req_end_timestamp.tv_usec = p_st_ems_packet->st_timestamp_last.tv_usec;
        }
        else if (p_st_sess->model_type == EMS_MODEL_TYPE_CONSUMER)
        {
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_NOTIFY".tibcoems.text", p_text == NULL ? "" : p_text) != FR_SUCC)
                goto error;

            // 上报数据
            if (ems_report_msg_data_req(p_st_conn, p_st_ems_packet, p_st_body, p_st_hash_report) != FR_SUCC)
                goto error;
        }
    }

    ems_free((void **) &p_inflated);
    ems_free((void **) &p_text);
    return FR_SUCC;
error:
    ems_free((void **) &p_inflated);
    ems_free((void **) &p_text);
    return FR_FAIL;
}
